import http from '../utils/request';

// 订单API前缀
const ORDER_API_PREFIX = '[Order]';

// 订单状态枚举
export enum OrderStatus {
  PENDING = 'PENDING',
  PROCESSING = 'processing',
  SHIPPED = 'shipped',
  DELIVERED = 'delivered',
  CANCELLED = 'cancelled',
  RETURNED = 'returned',
  UNPAID = 'NOTPAY',
  PAID = 'SUCCESS',
}

// 订单状态选项
export const ORDER_STATUS_OPTIONS = [
  { label: '待处理', value: OrderStatus.PENDING },
  { label: '处理中', value: OrderStatus.PROCESSING },
  { label: '已发货', value: OrderStatus.SHIPPED },
  { label: '已送达', value: OrderStatus.DELIVERED },
  { label: '已取消', value: OrderStatus.CANCELLED },
  { label: '已退货', value: OrderStatus.RETURNED },
  { label: '未支付', value: OrderStatus.UNPAID },
  { label: '已支付', value: OrderStatus.PAID },
];

// 订单类型定义
export interface Order {
  orderId: string;
  productId?: string;
  customerName: string;
  paymentType?: string;
  systemId?: string;
  customerAddress: string;
  customerEmail: string;
  productCode?: string;
  productDescription: string;
  amount: number;
  quantity: number;
  orderStatus: OrderStatus;
  orderDate: number;
  shippedDate?: number;
  trackingNumber?: string;
  paymentMethod?: string;
  transactionId?: string;
  transactionInfo?: string;
  totalAmount: number;
  userId?: string;
  IPAddress?: string;
  createTime: number;
  updateTime: number;
  product?: any;
  system?: any;
}

// 订单查询参数
export interface OrderQueryParams {
  orderId?: string;
  orderIds?: string;
  customerName?: string;
  orderStatus?: OrderStatus;
  paymentMethod?: string;
  productId?: string;
  systemId?: string;
  userId?: string;
  startDate?: number;
  endDate?: number;
  num?: number;
  page?: number;
}

// 创建订单数据
export interface CreateOrderData {
  productId?: string;
  customerName: string;
  paymentType?: string;
  systemId?: string;
  customerAddress: string;
  customerEmail: string;
  productCode?: string;
  productDescription: string;
  amount: number;
  quantity: number;
  orderStatus?: OrderStatus;
  paymentMethod?: string;
  userId?: string;
}

// 更新订单数据
export interface UpdateOrderData {
  customerName?: string;
  customerAddress?: string;
  customerEmail?: string;
  orderStatus?: OrderStatus;
  shippedDate?: number;
  trackingNumber?: string;
  paymentMethod?: string;
  amount?: number;
  quantity?: number;
}

// 订单列表响应
export interface OrderListResponse {
  msg: string;
  data: Order[];
  totalPage: number;
  totalCount: number;
  code?: string | number;
}

/**
 * 订单API
 */
export const orderApi = {
  /**
   * 获取订单列表（带筛选条件）
   * @param {OrderQueryParams} params - 查询参数
   * @returns {Promise<OrderListResponse>} - 订单列表响应
   */
  getOrderList: (params: OrderQueryParams = {}) => {
    console.log(`${ORDER_API_PREFIX} 获取订单列表`, params);
    const { num = 10, page = 1, ...filterParams } = params;
    
    // 构建筛选条件对象
    const selectParams: Partial<Order> = {};
    if (filterParams.orderId) selectParams.orderId = filterParams.orderId;
    if (filterParams.customerName) selectParams.customerName = filterParams.customerName;
    if (filterParams.orderStatus) selectParams.orderStatus = filterParams.orderStatus;
    if (filterParams.paymentMethod) selectParams.paymentMethod = filterParams.paymentMethod;
    if (filterParams.productId) selectParams.productId = filterParams.productId;
    if (filterParams.systemId) selectParams.systemId = filterParams.systemId;
    if (filterParams.userId) selectParams.userId = filterParams.userId;
    
    // 将 num 和 page 作为查询参数添加到 URL
    const url = `/orderss/select?num=${num}&page=${page}`;
    return http.post(url, selectParams, false, true, true, 'SUPER_ADMIN');
  },

  /**
   * 获取订单列表（通过订单ID列表）
   * @param {string} orderIds - 订单ID列表，逗号分隔
   * @param {number} num - 每页数量
   * @param {number} page - 页码
   * @returns {Promise<OrderListResponse>} - 订单列表响应
   */
  getOrderListByIds: (orderIds: string = '', num: number = 10, page: number = 1) => {
    console.log(`${ORDER_API_PREFIX} 通过订单ID列表获取订单`, orderIds);
    return http.get('/orderss/list', { orderIds, num, page }, false, true, 'SUPER_ADMIN');
  },

  /**
   * 获取订单详情
   * @param {string} orderId - 订单ID
   * @returns {Promise} - 订单详情
   */
  getOrderDetail: (orderId: string) => {
    console.log(`${ORDER_API_PREFIX} 获取订单详情:`, orderId);
    return http.get('/orderss/get', { orderId }, false, true, 'SUPER_ADMIN');
  },

  /**
   * 查询订单状态
   * @param {string} outTradeNo - 外部交易号或订单ID
   * @returns {Promise} - 订单查询结果
   */
  queryOrderStatus: (outTradeNo: string) => {
    console.log(`${ORDER_API_PREFIX} 查询订单状态:`, outTradeNo);
    return http.get('/orderss/query', { out_trade_no: outTradeNo, orderId: outTradeNo }, false, true, 'SUPER_ADMIN');
  },

  /**
   * 创建订单
   * @param {CreateOrderData} data - 订单数据
   * @returns {Promise} - 创建结果
   */
  createOrder: (data: CreateOrderData) => {
    console.log(`${ORDER_API_PREFIX} 创建订单`);
    return http.post('/orderss/createOrder', data, false, true, true, 'SUPER_ADMIN');
  },

  /**
   * 更新订单
   * @param {UpdateOrderData} data - 订单数据（需包含orderId）
   * @returns {Promise} - 更新结果
   */
  updateOrder: (data: UpdateOrderData & { orderId: string }) => {
    console.log(`${ORDER_API_PREFIX} 更新订单:`, data.orderId);
    return http.post('/orderss/update', data, false, true, true, 'SUPER_ADMIN');
  },
};

